/* ===================

AX2E (AXESCON XACML 2.0 Engine) is the Java authorization engine, which implements OASIS XACML 2.0 standard.
Copyright (C) 2007 AXESCON LLC

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


Contact: AXESCON LLC - info{at}axescon.com

 =================== */
package axescon.xacml.engine.alg;

import axescon.xacml.api.CtxHandler;
import axescon.xacml.api.XacmlException;
import axescon.xacml.api.alg.RuleCombAlg;
import axescon.xacml.engine.PolicyEvaluatorImpl;
import axescon.xacml.model.ctx.Result;
import axescon.xacml.model.policy.Rule;
import axescon.xacml.model.policy.RuleCombinerParameters;
import axescon.xacml.model.shared.Decision;


/**
 * @author argyn
 *         Date: Jun 27, 2005
 *         Time: 11:09:19 PM
 *         Responsibilities: represents XACML First applicable Rule Combining algorithm
 *         see spec 2.0 ch. C.5
 *         id: urn:oasis:names:tc:xacml:1.0:rule-combining-algorithm:first-applicable
 */
public class FirstApplicableRuleAlg implements RuleCombAlg {

    /**
     * The standard URN used to identify this algorithm
     */
    public static final String id =
            "urn:oasis:names:tc:xacml:1.0:rule-combining-algorithm:first-applicable";

    /**
     * Standard constructor.
     */
    public FirstApplicableRuleAlg() {
    }

    public axescon.xacml.model.ctx.Result combine(axescon.xacml.model.policy.Rule[] rules, axescon.xacml.model.ctx.XacmlCtx req,
                                         CtxHandler ch, RuleCombinerParameters[] params, PolicyEvaluatorImpl pdp)
            throws XacmlException {

        Result notappResult = null;
        for (int i = 0; i < rules.length; i++) {
            Rule rule = rules[i];
            Result result = pdp.getRuleEvaluator().eval(rule, ch, req);
            if (result.getDecision() != Decision.BYTE_NOT_APPLICABLE) return result;
            notappResult = result;
        }

        return notappResult;
    }

}
